package com.apes.fico.finance.service;

import com.apes.fico.engine.repository.BusinessDocumentRepository;
import com.apes.fico.engine.timerTask.DateUtil;
import com.apes.fico.finance.model.gl.GLVoucher;
import com.apes.fico.finance.model.gl.GLVoucherBatchDel;
import com.apes.fico.finance.respository.GLVoucherBatchDelRepository;
import com.apes.fico.finance.respository.GLVoucherRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Title: GLVoucherBatchDelService
 * @Description: 财务凭证批量删除
 * @author: ZhengZeXian
 * @create: 2022/3/14 15:34
 */
@Service("gLVoucherBatchDelService")
public class GLVoucherBatchDelService extends DomainService {

    @Autowired
    private GLVoucherBatchDelRepository glVoucherBatchDelRepository;
    @Autowired
    private GLVoucherRepository glVoucherRepository;
    @Autowired
    private GLVoucherService glVoucherService;

    /**
     * 保存
     * @param request
     */
    public GLVoucherBatchDel create(SimpleRequest request){
        return glVoucherBatchDelRepository.saveAndFlush(request.getO(GLVoucherBatchDel.class));
    }

    /**
     * 批量删除
     * @param request
     */
    public void delete(SimpleRequest request){
        GLVoucherBatchDel glVoucherBatchDel = request.getO(GLVoucherBatchDel.class);
        String period = glVoucherBatchDel.getPeriod();
        String company = glVoucherBatchDel.getCompany().getId();
        String voucherTemplate = glVoucherBatchDel.getVoucherTemplateId();
        Date startDate = glVoucherBatchDel.getStartDate();
        Date endDate = glVoucherBatchDel.getEndDate();
        List<GLVoucher> glVouchers = glVoucherRepository.findNoApproveVoucherByDate(period, company, voucherTemplate, "sent", startDate, endDate);
        glVouchers.forEach(item ->glVoucherService.deleteGLVoucher(item));
    }
}
